﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Member.BLL;
using Member.Model;
using Member.Content;
using Member.IBLL;

namespace Member.Controllers
{
    public class UsersMangeController : Controller
    {
        //private IUsersManagerBLL userbll = new UsersManagerBLL();
        private IUsersManageBLL userbll = new UsersManagerBLL();
        private int pagesize = 8;//页大小

        //
        // GET: /UsersMange/
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Load()
        {
            int pageindex = 1;
            if (Request["page"] != null) {
                try
                {
                    pageindex = int.Parse(Request["page"]);
                }catch(FormatException e){
                    pageindex = 1;
                }
            }
            List<User> list = userbll.GetModelsByPage(pagesize, pageindex, true, p => p.user_id, p => true).ToList();
            int max = userbll.GetModels(p => true).Count();
            int pagecount = (max - 1) / pagesize + 1;//总页数
            int nextpage = pageindex >= pagecount ? pagecount : pageindex + 1;//下一页
            int prevpage = pageindex == 1 ? 1 : pageindex - 1;//上一页
            if (pageindex >= pagecount) pageindex = pagecount;
            Page pages = new Page
            {
                PageCount = pagecount,
                NextPage = nextpage,
                PrevPage = prevpage,
                PageNo = pageindex
            };
            return Json(new { page=pages,list=list});
        }
        /// <summary>
        /// 增加
        /// </summary>
        /// <returns></returns>
        public JsonResult Add()
        {
            string json = Request["json"].ToString();
            Member.Model.User us = JsonTo.Deserialize<User>(json);
            us.user_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
            if (userbll.Add(us))
            {
                return Json(new { msg="用户添加成功！",s="ok"});
            }
            else
            {
                return Json(new { msg = "用户添加失败！", s = "no" });
            }
            
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        public JsonResult Del()
        {
            string json = Request["json"].ToString();
            List<User> ulist = JsonTo.JSONStringToList<User>(json);
            int count=0;
            foreach (var u in ulist)
            {
                if (userbll.delete(u.user_id))
                {
                    count++;
                }

            }
            //s表示状态，msg提示消息
            if (count == ulist.Count())
            {
                return Json(new { msg = count + "条数据全部删除成功啦", s = "ok" });

            }
            else
            {
                return Json(new { msg = count + "出现错误啦！只有" + count + "删除成功，还有" + (ulist.Count() - count) + "没有删除", s = "no" });
            }
            
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <returns></returns>
        public JsonResult Edit()
        {
            string json = Request["json"].ToString();
            Member.Model.User us = JsonTo.Deserialize<User>(json);
            if (userbll.Update(us))
            {
               return Json(new {msg="编辑成功",s="y"});
            }
            else
            {
               return Json(new { msg = "失败了，再来一次吧", s = "n" });
            }
        }
        /// <summary>
        /// 判断电话是否注册
        /// </summary>
        /// <returns></returns>
        public ActionResult istell()
        {
            string tell = Request["tell"].ToString();
            if (userbll.istell(tell))
            {
                return Json(new{s="n",msg="该手机号已经有人注册呢"});
            }
            else
            {
                return Json(new { s = "y" });
            }
        }
	}
}